package com.ziJin.controller;

import com.ziJin.pojo.Result;
import com.ziJin.pojo.Student;
import com.ziJin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 根据id查询学生信息
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }


    /**
     * 根据条件查询学生信息
     */
    @CrossOrigin(origins = "*")
    @GetMapping
    public Result findByInfo(@RequestParam(required = false) String studentNum,
                             @RequestParam(required = false) String name) {
        List<Student> studentList = studentService.findByInfo(studentNum, name);
        return Result.success(studentList);
    }

    /**
     * 查询所有学生信息
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/list")
    public Result findAll() {
        List<Student> studentList = studentService.findAll();
        return Result.success(studentList);
    }

    /**
     * 根据id删除学生
     */
    @CrossOrigin(origins = "*")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        studentService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增学生
     */
    @CrossOrigin(origins = "*")
    @PostMapping
    public Result save(@RequestBody Student student) {
        studentService.save(student);
        return Result.success();
    }

    /**
     * 更新学生信息
     */
    @CrossOrigin(origins = "*")
    @PutMapping
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }
}
